﻿@page "/"
@page "/home"
@using BlazorAppAuth.Components.Layout
@layout MainLayout
@rendermode InteractiveServer



<PageTitle>Home</PageTitle>

<style>
    .carousel-item {
        	height: 500px;
    }
</style>

<div class="row">
    <div class="border border-1 p-2">
        <p>当前用户</p>
        <span>@currentUserName</span>
    </div>
    <div class="border border-1 p-2 mt-4">
        <p>当前时间：</p>
        <span>@DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")</span>
    </div>
    <div class="border border-1 p-2 mt-4">
        <p>用户类型</p>
        <span>@currentUserRole</span>
    </div>
</div>


<div class="row row-cols-2 mt-4">

    <!---- 轮播 ---->
    <div id="imageCarousel" class="carousel slide" data-bs-ride="carousel" data-bs-interval="1000">
        <div class="carousel-inner">
            <div class="carousel-item active" >
                <img src="/images/1.jpg" class="d-block w-100" />
            </div>
            <div class="carousel-item" >
                <img src="/images/2.jpg" class="d-block w-100" />
            </div>
            <div class="carousel-item" >
                <img src="/images/3.jpg" class="d-block w-100" />
            </div>
        </div>
        <button class="carousel-control-prev" type="button" data-bs-target="#imageCarousel" data-bs-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="visually-hidden">Previous</span>
        </button>
        <button class="carousel-control-next" type="button" data-bs-target="#imageCarousel" data-bs-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="visually-hidden">Next</span>
        </button>
    </div>

    <div class="d-flex justify-content-around">
        <div class="card text-center mx-2 flex-grow-1">
            <div class="card-body">
                <p class="card-text" >运行总产量</p>
                <h1 class="card-title">@AllOpcuaCount 个</h1>
            </div>
        </div>
        <div class="card text-center mx-2 flex-grow-1">
            <div class="card-body">
                <p class="card-text">正品个数</p>
                <h1 class="card-title">@GoodOpcuaCount 个</h1>
            </div>
        </div>
        <div class="card text-center mx-2 flex-grow-1">
            <div class="card-body">
                <p class="card-text" >废品个数</p>
                <h1 class="card-title">@BadOpcuaCount 个</h1>
            </div>
        </div>
    </div>
</div>



@code {
    

    [CascadingParameter]
    MainLayout Layout { get; set; }

    // TODO 从数据库中获取
    private int AllOpcuaCount = 10;
    private int BadOpcuaCount = 2;
    private int GoodOpcuaCount = 8;

    private string currentUserName = "未登录";
    private string currentUserRole = "未知";

    [CascadingParameter]
    private Task<AuthenticationState> authenticationStateTask { get; set; }
    public async Task CheckLogin()
    {
        var authState = await authenticationStateTask;
        var user = authState.User;

        if (!user.Identity.IsAuthenticated)
        {
            NavigationManager.NavigateTo("/login");
        }

    }
    protected override async Task OnInitializedAsync()
    {
        await CheckLogin();
        var authState = await authenticationStateTask;
        var user = authState.User;

        if (user.Identity is not null && user.Identity.IsAuthenticated)
        {
            currentUserName = user.Identity.Name ?? "未知用户";
            currentUserRole = user.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Role)?.Value ?? "未知角色";
        }
    }
} 